package com.sync.syncmasterdata.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import nccloud.open.api.auto.token.factory.APIUtilsFactory;
import nccloud.open.api.auto.token.factory.NCCVersionEnum;
import nccloud.open.api.auto.token.itf.IAPIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BIP系统获取 token方法
 */
@Service
@Slf4j
public class BipRequestUtil {

    @Value("${bip-service.service-ip}")
    private String BIPIP;

    @Value("${bip-service.service-port}")
    private String BIPPORT;

    @Value("${bip-service.busi-center}")
    private String busiCenter;

    @Value("${bip-service.app-id}")
    private String appId;

    @Value("${bip-service.secret}")
    private String appSecret;

    @Value("${bip-service.public-key}")
    private String publicKey;


    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 返回BIP token对象-字符串
     * @return
     * @throws Exception
     */
    public String getBipAccessToken() throws Exception {
        IAPIUtils util = null;
        //配置版本
        util = APIUtilsFactory.getAPIUtils(NCCVersionEnum.YONBIP_PREMIUM);
        //初始化
        util.init(BIPIP, BIPPORT, busiCenter, appId, appSecret, publicKey, null, null);

        //获取token
        String token = util.getToken();
        log.info("打印token信息:{}", objectMapper.writeValueAsString(token));
        return token;
    }

    /**
     * 返回BIP token对象-字符串 (自定义Key)
     * @return
     * @throws Exception
     */
    public String getBipSelfAccessToken(String appIdSelf,String appSecretSelf,String publicKeySelf) throws Exception {
        IAPIUtils util = null;
        //配置版本
        util = APIUtilsFactory.getAPIUtils(NCCVersionEnum.YONBIP_PREMIUM);
        //初始化
        util.init(BIPIP, BIPPORT, busiCenter, appIdSelf, appSecretSelf, publicKeySelf, null, null);

        //获取token
        String token = util.getToken();
        return token;
    }

    /**
     * 返回初始化BIP apiutils
     * @return
     */
    public IAPIUtils getAPIUtil(){
        IAPIUtils iApiUtils = APIUtilsFactory.getAPIUtils(NCCVersionEnum.YONBIP_PREMIUM);
        iApiUtils.init(BIPIP, BIPPORT, busiCenter, appId, appSecret, publicKey, null, null);
        return iApiUtils;
    }

    /**
     * 返回初始化BIP apiUtils 自定义Appkey
     */
    public IAPIUtils getAPIUtil(String appIdSelf,String appSecretSelf,String publicKeySelf){
        IAPIUtils iApiUtils = APIUtilsFactory.getAPIUtils(NCCVersionEnum.YONBIP_PREMIUM);
        iApiUtils.init(BIPIP, BIPPORT, busiCenter, appIdSelf, appSecretSelf, publicKeySelf, null, null);
        return iApiUtils;
    }

    public Map<String, Object> requestMap(String keyName, Object valueObj) {
        Map<String, Object> map = new HashMap<>();
        map.put(keyName, valueObj);
        return map;
    }

}
